package falcon.helpers;
//TODO Test KML Writer
//TODO Add ground level line functionality

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.LinkedList;
import falcon.components.datatypes.Position;

/**
 * A class to write KML files for use with Google Earth/Maps
 * @author Ethan Harstad and Joe Coleman
 *
 */
public class KmlWriter {
	
	/**
	 * Write a KML file consisting of a 3D Line from the given list of positions.
	 * @param fileName
	 * @param positionList
	 * @param lineTitle
	 * @param lineColor
	 * @return
	 */
	public static boolean writeKml(String fileName, LinkedList<Position> positionList, String lineTitle, Color lineColor) {
		FileOutputStream file;
		try {
			file = new FileOutputStream(fileName);
		} catch (FileNotFoundException e) {
			System.err.println("Could not save file: " + fileName);
			return false;
		}
		PrintStream out = new PrintStream(file);
		writeKmlHeader(out, lineTitle, lineColor);
		for(Position p : positionList) {
			out.println("        " + p.getLongitude() + "," + p.getLatitude() + "," + p.getAltitude());
		}
		writeKmlFooter(out);
		return true;
	}
	
	/**
	 * Write a kml header to the given stream.
	 * @param stream
	 * @param title
	 * @param color
	 */
	private static void writeKmlHeader(PrintStream stream, String title, Color color) {
		stream.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		stream.println("<kml xmlns=\"http://earth.google.com/kml/2.1\">");
		stream.println("  <Document>");
		stream.println("    <name>" + title + "</name>");
		stream.println("    <description>HABET path generated by FALCON software. See http://falcon.sscl.iastate.edu for more information.</description>");
		stream.println("    <Style id=\"yellowLineGreenPoly\">");
		stream.println("      <LineStyle>");
		stream.println("        <color>" + Integer.toHexString(color.getRGB()) + "</color>");
		stream.println("        <width>4</width>");
		stream.println("      </LineStyle>");
		stream.println("      <PolyStyle>");
		stream.println("        <color>7f00ff00</color>");
		stream.println("      </PolyStyle>");
		stream.println("    </Style>");
		stream.println("    <Placemark>");
		stream.println("      <name>" + title + "</name>");
		stream.println("      <description>Transparent green wall with yellow outlines</description>");
		stream.println("      <styleUrl>#yellowLineGreenPoly</styleUrl>");
		stream.println("      <LineString>");
		stream.println("        <extrude>1</extrude>");
		stream.println("        <tessellate>1</tessellate>");
		stream.println("        <altitudeMode>absolute</altitudeMode>");
		stream.println("        <coordinates>");
	}
	
	/**
	 * Write a kml footer to the given stream.
	 * @param stream
	 */
	private static void writeKmlFooter(PrintStream stream) {
		stream.println("        </coordinates>");
		stream.println("      </LineString>");
		stream.println("    </Placemark>");
		stream.println("  </Document>");
		stream.println("</kml>	");
	}

}
